import request from '../request/request';
import type { DocumentQueryParams, Document } from '../models/document';

/**
 * 创建新文档
 */
export const createDocument = async (params: {
  doc_title: string;
  doc_type: number;
  team_id?: number;
  doc_content?: string;
  permission?: number;
}) => {
  return await request.post('/document/create', params);
};

/**
 * 获取文档列表
 */
export const getDocumentList = async (params?: DocumentQueryParams) => {
  return await request.get('/document/list', { params });
};

/**
 * 获取文档详情
 */
export const getDocumentDetail = async (docId: number) => {
  return await request.get(`/document/detail/${docId}`);
};

/**
 * 更新文档内容
 */
export const updateDocument = async (params: {
  id: number;
  doc_title?: string;
  doc_content?: string;
  permission?: number;
  edit_note?: string;
}) => {
  return await request.post('/document/update', params);
};

/**
 * 删除文档
 */
export const deleteDocument = async (docId: number) => {
  return await request.post('/document/delete', { id: docId });
};

/**
 * 锁定/解锁文档
 */
export const lockDocument = async (docId: number, lockStatus: number) => {
  return await request.post('/document/lock', { id: docId, edit_status: lockStatus });
};

/**
 * 获取文档版本列表
 */
export const getDocumentVersions = async (docId: number) => {
  return await request.get(`/document/versions/${docId}`);
};

/**
 * 获取指定版本的文档内容
 */
export const getDocumentVersion = async (docId: number, version: number) => {
  return await request.get(`/document/version/${docId}/${version}`);
};

/**
 * 恢复到指定版本
 */
export const revertDocumentVersion = async (params: {
  doc_id: number;
  version: number;
}) => {
  return await request.post('/document/revert', params);
};

/**
 * 搜索文档
 */
export const searchDocuments = async (keyword: string, params?: {
  team_id?: number;
  doc_type?: number;
}) => {
  return await request.get('/document/search', { params: { keyword, ...params } });
};

/**
 * 获取团队文档统计信息
 */
export const getTeamDocumentStats = async (teamId: number) => {
  return await request.get(`/document/team/stats/${teamId}`);
};